package first.niuke.simple;

import java.util.HashMap;

/**
 * @ClassName NC156
 * @description:
 * @author: 荣燊
 * @create: 2021-06-01 19:55
 **/
public class NC156 {
    // 数组中自出现一次的数
    public static void main(String[] args) {
        int[] arr = {5, 4, 1, 1, 5, 1, 5};
        int k = 3;
        System.out.println(foundOnceNumber(arr, k));
    }

    public static int foundOnceNumber (int[] arr, int k) {
        // write code here
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i=0; i<arr.length; i++) {
            if (map.containsKey(arr[i])) {
                map.put(arr[i], map.get(arr[i])+1);
            } else {
                map.put(arr[i], 1);
            }
        }
        for (int j=0; j<arr.length; j++) {
            // map.get(arr[j]) != k 为什么通不过测试用例？？？
            if (map.get(arr[j]) == 1) {
                return arr[j];
            }
        }
        return 0;
    }
}
